package com.dpiinc.TechPage.api

import com.dpiinc.TechPage.UserManager
import com.dpiinc.TechPage.model.BaseEntity
import com.dpiinc.TechPage.model.NoteListBean
import com.beiji.lib.pen.PenLog
import io.reactivex.Observable

/**
 * 笔记列表相关API
 * Created by X on 2018/7/1.
 */
class NoteListApi {

    companion object {
        private const val LIST_TYPE_DEFAULT = 0
        private const val LIST_TYPE_SEARCH_BY_CONTENT = 1
        private const val LIST_TYPE_SEARCH_BY_LABEL = 2

        fun create(): NoteListApi = NoteListApi()
    }

    /**
     * 加载最新的number条数据
     */
    fun loadFirstPage(pageSize: Int): Observable<BaseEntity<NoteListBean>> {
        PenLog.d("loadFirstPage")
        val params = HashMap<String, String>()
        params["uId"] = UserManager.getUserId()
        params["accessToken"] = UserManager.getAccessToken()
        params["number"] = pageSize.toString()
        params["listType"] = LIST_TYPE_DEFAULT.toString()
        return RetrofitFactory.getInstance().getNoteList(params)
    }

    /**
     * 加载比noteId对应的笔记更早的number条数据
     */
    fun loadDataBefore(pageSize: Int, noteId: String): Observable<BaseEntity<NoteListBean>> {
        PenLog.d("loadDataBefore")
        val params = HashMap<String, String>()
        params["uId"] = UserManager.getUserId()
        params["accessToken"] = UserManager.getAccessToken()
        params["number"] = pageSize.toString()
        params["listType"] = LIST_TYPE_DEFAULT.toString()
        params["beforeNoteId"] = noteId
        return RetrofitFactory.getInstance().getNoteList(params)
    }

    /**
     * 检查数据更新（新增、修改、删除）
     * @param latestGetTime 上一次更新列表的时间
     */
    fun loadUpdate(pageSize: Int, latestGetTime: Long): Observable<BaseEntity<NoteListBean>> {
        PenLog.d("loadUpdate")
        val params = HashMap<String, String>()
        params["uId"] = UserManager.getUserId()
        params["accessToken"] = UserManager.getAccessToken()
        params["number"] = pageSize.toString()
        params["listType"] = LIST_TYPE_DEFAULT.toString()
        params["latestGetTime"] = latestGetTime.toString()
        return RetrofitFactory.getInstance().getNoteList(params)
    }

    /**
     * 按关键字搜索
     */
    fun searchByKeyword(pageSize: Int, keyword: String): Observable<BaseEntity<NoteListBean>> {
        PenLog.d("searchByKeyword")
        val params = HashMap<String, String>()
        params["uId"] = UserManager.getUserId()
        params["accessToken"] = UserManager.getAccessToken()
        params["number"] = pageSize.toString()
        params["listType"] = LIST_TYPE_SEARCH_BY_CONTENT.toString()
        params["filterContent"] = keyword
        return RetrofitFactory.getInstance().getNoteList(params)
    }

    /**
     * 按关键字搜索,加载更多，获取比noteId更早的一页
     */
    fun loadMoreByKeyword(pageSize: Int, keyword: String, noteId: String): Observable<BaseEntity<NoteListBean>> {
        PenLog.d("loadMoreByKeyword")
        val params = HashMap<String, String>()
        params["uId"] = UserManager.getUserId()
        params["accessToken"] = UserManager.getAccessToken()
        params["number"] = pageSize.toString()
        params["listType"] = LIST_TYPE_SEARCH_BY_CONTENT.toString()
        params["filterContent"] = keyword
        params["beforeNoteId"] = noteId
        return RetrofitFactory.getInstance().getNoteList(params)
    }

    /**
     * 按标签搜索
     */
    fun searchByLabel(pageSize: Int, labelId: String): Observable<BaseEntity<NoteListBean>> {
        PenLog.d("searchByLabel")
        val params = HashMap<String, String>()
        params["uId"] = UserManager.getUserId()
        params["accessToken"] = UserManager.getAccessToken()
        params["number"] = pageSize.toString()
        params["listType"] = LIST_TYPE_SEARCH_BY_LABEL.toString()
        params["filterContent"] = labelId
        return RetrofitFactory.getInstance().getNoteList(params)
    }

    /**
     * 按标签搜索,加载更多，获取比noteId更早的一页
     */
    fun loadMoreByLabel(pageSize: Int, labelId: String, noteId: String): Observable<BaseEntity<NoteListBean>> {
        PenLog.d("loadMoreByLabel")
        val params = HashMap<String, String>()
        params["uId"] = UserManager.getUserId()
        params["accessToken"] = UserManager.getAccessToken()
        params["number"] = pageSize.toString()
        params["listType"] = LIST_TYPE_SEARCH_BY_LABEL.toString()
        params["filterContent"] = labelId
        params["beforeNoteId"] = noteId
        return RetrofitFactory.getInstance().getNoteList(params)
    }


}